package com.kokomi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kokomi.pojo.Book;
import com.kokomi.pojo.MyPageData;
import com.kokomi.pojo.RedisConst;
import com.kokomi.pojo.Result;
import com.kokomi.pojo.VO.BookTypeVO;
import com.kokomi.pojo.VO.BookVO;
import com.kokomi.pojo.enumeration.BusinessCode;
import com.kokomi.service.BookService;
import com.kokomi.service.BookTypeService;
import com.kokomi.util.ProductionCaptchaUtil;
import com.kokomi.util.SmsUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
public class BookController {
    private final BookService bookService;
    private final BookTypeService bookTypeService;
    private final RedisTemplate<String, String> redisTemplate;
    private final SmsUtil smsUtil;

    @GetMapping("/getBookList")
    //@Cacheable(value = RedisConst.BOOK_LIST,keyGenerator = "keyGenerator")
    public List<Book> getBookList() {
        return bookService.list();
    }

    @GetMapping("/getBookType")
    @Cacheable(value = RedisConst.BOOK_TYPE_LIST,keyGenerator = "keyGenerator")
    public Result<List<BookTypeVO>> getBookType() {
        //新建一个集合
        List<BookTypeVO> bookTypes = new ArrayList<>();

        // 获取图书id和名字
        bookTypeService.list().forEach(book -> {
          //新建一个对象
          BookTypeVO bookType = new BookTypeVO();
          bookType.setId(book.getId());
          bookType.setName(book.getName());
          bookTypes.add(bookType);
        });
        return Result.ok(bookTypes, "获取图书类型成功",200);

    }

    @PostMapping("/getBookByTypeId/{TypeId}/{pageNum}/{pageSize}")
    @Cacheable(value = RedisConst.BOOK_LIST,keyGenerator = "keyGenerator")
    public Result<MyPageData> getBookByTypeId(
            @PathVariable("TypeId") Integer TypeId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize

    ) {
        Page<Book> page = new Page<>(pageNum,pageSize);

        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("type_id",TypeId);
        bookService.page(page, wrapper);
        List<Book> books = page.getRecords();
        List<BookVO> vos = new ArrayList<>();
        books.forEach(book -> {
            BookVO bookVO = new BookVO();
            BeanUtils.copyProperties(book,bookVO);
            vos.add(bookVO);
        });
        MyPageData myPageData = new MyPageData(page.getTotal(),vos);
        return Result.ok(myPageData,"获取图书成功",200);
    }
    @PostMapping("/getBookBySales/{pageNum}/{pageSize}")
    @Cacheable(value = RedisConst.BOOK_LIST,keyGenerator = "keyGenerator")
    public Result<MyPageData> getBookBySales(
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    )
    {
        // 根据销量排序
        Page<Book> page = new Page<>(pageNum,pageSize);
        Page<Book> buyCount = bookService.page(page, new QueryWrapper<Book>().orderByDesc("buy_count"));
        MyPageData myPageData = new MyPageData();
        myPageData.setTotal(buyCount.getTotal());
        myPageData.setData(buyCount.getRecords());
        return Result.ok(myPageData,"获取图书成功",200);
    }

    @GetMapping("/getBookById/{Tid}")
    public Result<Book> getBookById(@PathVariable("Tid") Integer Tid) {
        return Result.ok(bookService.getById(Tid),"获取图书成功",200);
    }

    @GetMapping("/getBookBySearch/{Search}/{pageNum}/{pageSize}")
    public Result<MyPageData> getBookBySearch(
            @PathVariable("Search") String Search,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){
        // 根据书名和作者查询书籍
        Page<Book> page = new Page<>(pageNum,pageSize);
        Page<Book> bookPage = bookService.page
                (page, new QueryWrapper<Book>()
                        .select("id","name","author","price","img_src","buy_count")
                        .like("name", Search)
                        .or()
                        .like("author", Search));
        MyPageData myPageData = new MyPageData();
        myPageData.setTotal(bookPage.getTotal());
        myPageData.setData(bookPage.getRecords());
        return Result.ok(myPageData,"获取图书成功",200);

    }




}
